<template>
	<view class="category" :class="theme+'-bg-fenl'">

		<scroll-view scroll-y="true" style="height: 100%;">

			<navigator url="/pages/category/search/Search">
				<uni-search-bar :placeholder="$t('Search.Entersearchcontent')" :disabled="true" cancelButton="none" />
			</navigator>

			<view class="link" :class="theme+'-bg-hbox'">
				<view class="nav" @click="goWhole">
					<view class="link-l br" :class="[`${theme}-ftext-color`,`${theme}-border-right`]">
						<view class="icon">
							<image src="../../static/images/icons/quanbu.png"></image>
						</view>
						<text>
							{{$t('category.Alltools')}}
						</text>
					</view>
				</view>

				<view class="nav" @click="goMyAccount">
					<view class="link-l" :class="[`${theme}-ftext-color`]">
						<view class="icon con">
							<image src="../../static/images/icons/fengche.png"></image>
						</view>
						<text>
							{{$t('category.Member')}}
						</text>

					</view>
				</view>

			</view>
			<view class="nav">
				<uni-collapse>
					<uni-collapse-item :thumb="item.icon" :length="item.list.length" :show-animation="true"
						:title="locale=='en'?item.english:item.title" v-for="item in list" :key="item.id"
						v-if="item.if_show">
						<view class="toolLink">
							<text class="br-4" :class="[`${theme}-bg-cbox`,`${theme}-ftext-color`]"
								v-for="ele in item.list" @click="go(ele.url)" :key="ele.id"
								@longpress="open(ele.id)">{{locale=='en'?ele.english:ele.title}}</text>
						</view>
					</uni-collapse-item>
				</uni-collapse>
			</view>

			<uni-popup ref="popup" type="bottom">
				<view class="popupBox br-1" :class="theme+'-bg-cbox'">
					<view class="item" :class="theme + '-ftext-color'">
						<text class="title">{{$t('category.Options')}}</text>
					</view>
					<view class="item line" :class="theme + '-ftext-color'" @click="collect" v-if="!isShow">
						<view class="icon">
							<image src="../../static/images/img/light-meiyoushoucang.png">
							</image>
						</view>
						<view class="text">
							<text>{{$t('category.Collect')}}</text>
						</view>

					</view>
					<view class="item line" :class="theme + '-ftext-color'" @click="collect" v-if="isShow">
						<view class="icon">
							<image src="../../static/images/img/light-meiyoushoucang.png">
							</image>
						</view>
						<view class="text">
							<text>{{$t('category.Unfavorite')}}</text>
						</view>

					</view>
				</view>
			</uni-popup>
		</scroll-view>

	</view>
</template>

<script>
	export default {
		data() {
			return {
				list: [],
				toolsList: [],
				collectList: [],
				id: '',
				isShow: false
			}
		},
		onLoad() {
			this.$theme()
		},
		onShow() {
			this.$theme()
			this.getData()
		},
		methods: {
			go(link) {
				uni.navigateTo({
					url: link
				})
			},
			getData() {
				uni.getStorage({
					key: 'collectList',
					success: (res) => {
						this.collectList = res.data
					}
				})

				this.$get('/category/list').then((res) => {
					res.data.forEach(item => {
						item.list.forEach((ele) => {
							ele.isCheckd = false
							ele.isCollection = false
							this.collectList.forEach((it) => {
								if (ele.id == it.id) {
									ele.isCollection = true
								}
							})
						})
					})

					this.list = res.data
				})
			},
			open(id) {
				this.$refs.popup.open()
				this.id = id
				this.list.forEach(item => {
					item.list.forEach(ele => {
						if (ele.id == this.id) {
							if (ele.isCollection == false) {
								this.isShow = false
							} else {
								this.isShow = true
							}
						}
					})

				})
			},
			close() {
				this.$refs.popup.close()
			},
			collect() {
				if (!uni.getStorageSync('userInformation').token) {
					// #ifdef APP-PLUS
					return uni.showToast({
						title: '需要登录才能收藏哦。',
						icon: 'none',
						success: () => {
							setTimeout(() => {
								uni.navigateTo({
									url: '/pages/mine/my-account/login/Login'
								})
							}, 500)
						}
					})
					// #endif
					// #ifdef MP-WEIXIN
					uni.showToast({
						title: '还未登录呢，请先登录',
						icon: 'none',
						success: () => {},
					})
					this.weChatLogin()
					return
					// #endif
				}

				this.$post('/tool/collect', {
					toolId: this.id, //1,2 工具的id，多个工具以英文逗号分割
					type: 1, //1：收藏；2点赞
					action: 1 //0：取消收藏/点赞；1：收藏/点赞
				})

				this.list.forEach(item => {
					item.list.forEach(ele => {
						if (ele.id == this.id) {
							if (ele.isCollection == false) {
								ele.isCollection = true
								this.collectList.unshift(ele)
								uni.showToast({
									icon: 'none',
									title: '收藏成功'
								})
							} else {
								ele.isCollection = false
								this.collectList = this.collectList.filter(el => el.id != this.id)
								uni.showToast({
									icon: 'none',
									title: '取消收藏'
								})
							}

							uni.setStorage({
								key: 'collectList',
								data: this.collectList,
								success: () => {
									this.getData()
								}
							})

							this.close()
						}
					})

				})
			},
			goWhole() {
				uni.navigateTo({
					url: './whole/Whole',
				})
			},
			goMyAccount() {
				if (!uni.getStorageSync('userInformation')) {
					// #ifdef H5
					uni.showToast({
						title: '还未登录呢，即将跳转登录',
						icon: 'none'
					})
					setTimeout(() => {
						uni.navigateTo({
							url: '/pages/mine/my-account/login/Login',
						})
					}, 500)
					// #endif
					// #ifdef APP-PLUS
					uni.showToast({
						title: '还未登录呢，即将跳转登录',
						icon: 'none'
					})
					setTimeout(() => {
						uni.navigateTo({
							url: '/pages/mine/my-account/login/Login',
						})
					}, 500)
					// #endif
					// #ifdef MP-WEIXIN
					uni.showToast({
						title: '还未登录呢，请先登录',
						icon: 'none'
					})
					this.weChatLogin()
					// #endif
				} else {
					uni.navigateTo({
						url: '/pages/mine/my-account/MyAccount'
					})
				}

			},
			weChatLogin() {
				//#ifdef MP-WEIXIN
				uni.checkSession({
					success: (res) => {
						console.log(res)
					},
					fail: (err) => {
						console.log(err)
					}
				})
				uni.getUserProfile({
					desc: '登录',
					success: (res1) => {
						// console.log(res1.userInfo)
						uni.showLoading({
							title: '登录中...'
						})
						uni.getProvider({
							service: 'oauth',
							success: (res2) => {
								// console.log(res2)
								if (~res2.provider.indexOf('weixin')) {
									uni.login({
										provider: 'weixin',
										success: (res3) => {
											// console.log(res3)

											this.$post('/login/wx-mini', {
												code: res3.code
											}).then(res4 => {

												// console.log(res4.data)
												uni.hideLoading()
												uni.showToast({
													title: "登录成功",
													icon: 'none'
												});
												res4.data.nickname = res1
													.userInfo.nickName
												this.userInformation = res4
													.data
												uni.setStorage({
													key: 'userInformation',
													data: res4.data
												})
											})
										},
										fail: () => {
											uni.showToast({
												title: "微信登录授权失败",
												icon: "none"
											});
										}
									})

								} else {
									uni.showToast({
										title: '请先安装微信或升级版本',
										icon: "none"
									});
								}
							}
						});
					},
					fail: () => {
						// console.log(777)
						// uni.showToast({
						// 	title: "微信登录授权失败",
						// 	icon: "none"
						// });
					}
				})
				//#endif
			}
		}
	}
</script>

<style lang="scss" scoped>
	.category {
		height: 100%;

		.link {
			height: 120rpx;
			display: flex;
			justify-content: center;
			align-items: center;
			margin-bottom: 15rpx;

			.nav {
				flex: 1;
				height: 120rpx;
				display: flex;
				justify-content: center;
				align-items: center;
				cursor: pointer;
			}

			.link-l {
				flex: 1;
				height: 44rpx;
				display: flex;
				justify-content: center;
				align-items: center;
				font-size: 30rpx;

				.icon {
					width: 40rpx;
					height: 40rpx;
					margin-right: 10rpx;
				}

				.con {
					width: 48rpx;
					height: 48rpx;
				}

				text {
					cursor: pointer;
				}
			}

		}

		.nav {

			.toolLink {
				padding: 0 0 0 36rpx;
				display: flex;
				flex-wrap: wrap;

				text /deep/ {
					display: block;
					padding: 20rpx 25rpx;
					// background: #F6F6F4;
					font-size: 24rpx;
					margin: 0 20rpx 25rpx 0;
					cursor: pointer;
				}

				// text:active {
				// 	background: $uni-bg-color-hover;
				// }
			}
		}

		.popupBox {
			display: flex;
			flex-direction: column;
			height: 300rpx;

			.line {
				cursor: pointer;
			}

			.item {
				width: 100%;
				height: 100rpx;
				display: flex;
				align-items: center;

				.icon {
					margin-left: 40rpx;
				}

				.title {
					font-size: 34rpx;
					font-weight: 600;
					margin-left: 40rpx;
				}

				text {
					margin-left: 40rpx;
					font-size: 24rpx;
					cursor: pointer;
				}
			}
		}
	}
</style>
